package com.lfz.demo.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lfz.demo.dao.RoleMapper;
import com.lfz.demo.dao.UserMapper;
import com.lfz.demo.entity.User;
import com.lfz.demo.repository.UserRepository;
import com.lfz.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.Map;


/**
 * @author lfz
 * @date 2025/3/3 9:35
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserRepository userRepository;

    @Override
    public Page<User> findByPage(Specification<User> spec, Pageable pageable) {
        return userRepository.findAll(spec, pageable);
    }

    @Override
    public User login(String username, String password) {
        return userMapper.login(username, password);
    }

    @Override
    public void loadUserByLeftJoin(String username, Integer page, Integer limit) {
        userMapper.loadUserByLeftJoin(username, page, limit);
    }

    @Override
    public void saveUserRole(Integer uid, Integer[] ids) {
        roleMapper.deleteUserRoleByUid(uid);
        if (ids!=null&&ids.length>0){
            for (Integer rid : ids){
                roleMapper.saveUserRole(uid, rid);
            }
        }
    }

    @Override
    public void AssignPermissionsByLoadUserIdDescOne() {
        userMapper.AssignPermissionsByLoadUserIdDescOne();
    }

    @Override
    public int countByUsername(String username) {
        return userMapper.countByUsername(username);
    }

    @Override
    public String phoneByUsername(String username) {
        return userMapper.phoneByUsername(username);
    }

    @Override
    public Map<String, String> infoByUsername(String username) {
        return userMapper.infoByUsername(username);
    }
}
